<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iterator</title>
</head>

<body>
    <script>
        let obj = {
            id: '123',
            name: '张三',
            age: 18,
            gender: '男',
            hobbie: '睡觉'
        }
        obj[Symbol.iterator] = function () {
            let keys = Object.keys(obj)
            let index = 0
            return {
                next() {
                    let done = index >= keys.length
                    return {
                        done,
                        value: done ? void 0 : {
                            key: keys[index],
                            val: obj[keys[index++]]
                        }
                    }
                }
            }
        }

        for (const key of obj) {
            console.log(key);
        }
    </script>
</body>

</html>